package com.lang.generator.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.lang.generator.core.helper.MockDataHelper;
import com.lang.generator.core.helper.SqlGeneratorHelper;
import com.lang.generator.core.helper.SqlParserHelper;
import com.lang.generator.domain.dto.ContentDto;
import com.lang.generator.domain.dto.DataModelDto;
import com.lang.generator.domain.dto.DataModelParseDto;
import com.lang.generator.domain.dto.TemplateDto;
import com.lang.generator.domain.entity.TableStructure;
import com.lang.generator.domain.enums.TemplateTypeEnum;
import com.lang.generator.domain.vo.GeneratorVo;
import com.lang.generator.service.GeneratorService;
import com.lang.generator.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipOutputStream;

/**
 * 生成器服务impl
 *
 * @author Lang
 * @date 2022/11/19
 */
@Service
@Slf4j
public class GeneratorServiceImpl implements GeneratorService {
    @Resource
    private SqlParserHelper sqlParserHelper;
    @Resource
    private MockDataHelper mockDataHelper;
    @Resource
    private SqlGeneratorHelper sqlGeneratorHelper;
    @Resource
    private TemplateService templateService;

    /**
     * 代码生成器
     *
     * @param generatorVo 发电机签证官
     * @return {@link List}<{@link ContentDto}>
     */
    @Override
    public List<ContentDto> generatorCode(GeneratorVo generatorVo) {
        if (CollectionUtil.isEmpty(generatorVo.getTemplateList())) {
            List<TemplateDto> defaultTemplateList;
            if (StrUtil.isBlank(generatorVo.getTemplateType())){
                defaultTemplateList = templateService.getDefaultTemplate(null);
            }else {
                defaultTemplateList = templateService.getDefaultTemplate(TemplateTypeEnum.valueOf(generatorVo.getTemplateType()));
            }
            generatorVo.setTemplateList(defaultTemplateList);
        }
        List<TemplateDto> templateDtoList = generatorVo.getTemplateList();
        List<DataModelParseDto> dataModelList = getDataModel(generatorVo);
        dataModelList.forEach(e -> sqlParserHelper.formatTableStructure(e.getTableStructure()));
        return templateService.parseTemplate(templateDtoList, dataModelList);
    }

    /**
     * zip包代码
     *
     * @param contentDtoList 内容dto列表
     * @return {@link byte[]}
     */
    @Override
    public byte[] packageCodeZip(List<ContentDto> contentDtoList) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
        contentDtoList.forEach(e -> templateService.appendZipStream(zipOutputStream, e));
        IoUtil.close(zipOutputStream);
        return outputStream.toByteArray();
    }

    /**
     * 发电机模拟sql
     *
     * @param tableStructure 表结构
     * @param rowNum         行num
     * @return {@link String}
     */
    @Override
    public String generatorMockSql(TableStructure tableStructure, Integer rowNum) {
        List<Map<String, Object>> maps = mockDataHelper.generateMockData(tableStructure, rowNum);
        return sqlGeneratorHelper.buildInsertSql(tableStructure, maps);
    }


    /**
     * 得到数据模型
     *
     * @param generatorVo 发电机签证官
     * @return {@link List}<{@link DataModelParseDto}>
     */
    private List<DataModelParseDto> getDataModel(GeneratorVo generatorVo) {
        DataModelDto dataModelDto = generatorVo.getDataModelDto();
        return dataModelDto.getTableStructures().stream().map(e -> {
            DataModelParseDto dataModelParseDto = new DataModelParseDto();
            BeanUtil.copyProperties(dataModelDto, dataModelParseDto);
            dataModelParseDto.setTableStructure(e);
            return dataModelParseDto;
        }).collect(Collectors.toList());
    }
}
